/**
 * 作者：陈文斌 on 2019/1/11.
 * 邮箱：chen_wennbin@163.com
 * 版本：v1.0
 */

import 'package:flutter/material.dart';
import 'package:crm/constant/constant.dart';
import 'package:crm/model/main_scoped_model.dart';
import 'package:crm/model/ClientManager/contact_info_model.dart';
import 'package:crm/view/Tile/list_tile_text_field.dart';
import 'package:crm/view/AppBar/custom_app_bar.dart';

const String Title = '联系人名片';

class ContactCartEditPage extends StatefulWidget {
  final ContactInfoModel contactInfo;

  @override
  ContactCartEditPage({Key key, this.contactInfo}) : super(key: key);

  State<StatefulWidget> createState() => _ContactCartEditPageState();
}

class _ContactCartEditPageState extends State<ContactCartEditPage> {
  final TextStyle _titleStyle = TextStyle(
    color: AppColors.DetailTextColor,
    fontSize: AppFontSizes.Font15,
    fontFamily: AppFontFamilies.PingFangSC,
    fontWeight: FontWeight.w500,
  );

  /// 创建各个焦点对象
  final FocusNode _nameFocusNode = FocusNode();
  final FocusNode _positionFocusNode = FocusNode();
  final FocusNode _phoneFocusNode = FocusNode();
  final FocusNode _telephoneFocusNode = FocusNode();
  final FocusNode _QQAccountFocusNode = FocusNode();
  final FocusNode _mailFocusNode = FocusNode();

  /// 创建一个联系人对象接受final类型的
  ContactInfoModel _contactInfo;

  @override
  Widget build(BuildContext context) {
    MainModel contactManagerSM = MainModel().of(context);
    _contactInfo = widget.contactInfo ?? ContactInfoModel.fromJson({});

    Widget ContactCartEditListSection(ContactInfoModel contactInfo) {
      return Column(
        children: <Widget>[
          ListTileTextField(
            title: Text('姓名'),
            content: contactInfo.name ?? '',
            focusNode: _nameFocusNode,
            textInputAction: TextInputAction.next,
            onEditingComplete: () =>
                FocusScope.of(context).requestFocus(_positionFocusNode),
            onChanged: (name) {
              contactInfo.name = name;
            },
          ),
          ListTileTextField(
            title: Text('职位'),
            content: contactInfo.position ?? '',
            focusNode: _positionFocusNode,
            textInputAction: TextInputAction.next,
            onEditingComplete: () =>
                FocusScope.of(context).requestFocus(_phoneFocusNode),
            onChanged: (position) {
              contactInfo.position = position;
            },
          ),
          ListTileTextField(
            title: Text('手机'),
            content: contactInfo.phone ?? '',
            focusNode: _phoneFocusNode,
            textInputAction: TextInputAction.next,
            onEditingComplete: () =>
                FocusScope.of(context).requestFocus(_telephoneFocusNode),
            onChanged: (phone) {
              contactInfo.phone = phone;
            },
          ),
          ListTileTextField(
            title: Text('座机'),
            content: contactInfo.telephone ?? '',
            focusNode: _telephoneFocusNode,
            textInputAction: TextInputAction.next,
            onEditingComplete: () =>
                FocusScope.of(context).requestFocus(_QQAccountFocusNode),
            onChanged: (telephone) {
              contactInfo.telephone = telephone;
            },
          ),
          ListTileTextField(
            title: Text('QQ'),
            content: contactInfo.QQaccount ?? '',
            focusNode: _QQAccountFocusNode,
            textInputAction: TextInputAction.next,
            onEditingComplete: () =>
                FocusScope.of(context).requestFocus(_mailFocusNode),
            onChanged: (QQAccount) {
              contactInfo.QQaccount = QQAccount;
            },
          ),
          ListTileTextField(
            title: Text('邮箱'),
            content: contactInfo.mail ?? '',
            focusNode: _mailFocusNode,
            onEditingComplete: () =>
                FocusScope.of(context).requestFocus(FocusNode()),
            onChanged: (mail) {
              contactInfo.mail = mail;
            },
          ),
        ],
      );
    }

    return Scaffold(
      resizeToAvoidBottomInset: false,
      backgroundColor: AppColors.PageBackgroundColor,
      appBar: CustomAppBar(
        title: Title,
        actions: <Widget>[
          MaterialButton(
            onPressed: () {
              contactManagerSM.removeContact(_contactInfo);
              Navigator.pop(context);
            },
            child: Text(
              '删除',
              style: AppStyles.actionsTitleStyle,
            ),
            splashColor: Colors.transparent,
            highlightColor: Colors.transparent,
          )
        ],
      ),
      body: GestureDetector(
        onTap: () {
          FocusScope.of(context).requestFocus(FocusNode());
        },
        child: ListView(
          children: <Widget>[
            Padding(padding: EdgeInsets.only(top: 7)),
            ContactCartEditListSection(_contactInfo),
            Container(
              width: AppScreen.setWidth(318),
              height: 40,
              margin: EdgeInsets.fromLTRB(
                AppScreen.setWidth(28),
                100,
                AppScreen.setWidth(29),
                20,
              ),
              decoration: BoxDecoration(
                color: AppColors.MainColor,
                borderRadius: BorderRadius.circular(20),
              ),
              child: ClipRRect(
                borderRadius: BorderRadius.circular(20),
                child: MaterialButton(
                  onPressed: () {
                    print(_contactInfo.toString());
                  },
                  child: Text(
                    '保存',
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: AppFontSizes.Font17,
                      fontFamily: AppFontFamilies.PingFangSC,
                    ),
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
